namespace LightBox.Hosting
{
	using System;
	using System.IO;
	using System.Web.Hosting;

	using Providers;

	/// <summary>
	/// Common instance of a virtual file for LightBox.
	/// </summary>
	public class ImageVirtualFile : VirtualFile
	{
		private ImageProvider provider;

		/// <summary>
		/// Public constructor.
		/// </summary>
		/// <param name="virtualPath">Path to the virtual image.</param>
		/// <param name="imgProvider">ImageProvider to retrieve the image.</param>
		public ImageVirtualFile(string virtualPath, ImageProvider imgProvider)
			: base(virtualPath)
		{
			this.provider = imgProvider;
		}

		/// <summary>
		/// Gets the stream associated with the image.
		/// </summary>
		/// <returns></returns>
		public override System.IO.Stream Open()
		{
			Stream strm = null;

			if (this.provider != null)
			{
				strm = this.provider.GetImageByUrl(this.VirtualPath);
			}

			return strm;
		}
	}
}
